package cj.web.vote.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.web.admin.domain.MemuInfo;
import cj.web.admin.domain.RoleInfo;
import cj.web.admin.domain.RoleMenuRef;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;
import cj.web.vote.dao.VoteDAO;
import cj.web.vote.dao.VoteSongRefDAO;
import cj.web.vote.domain.Song;
import cj.web.vote.domain.Vote;
import cj.web.vote.domain.VoteSongRef;

/**
 * 投票管理
 * @author Administrator
 *
 */
@Path("/vote")
public class VoteController extends BaseController{
	
	@Autowired
	private VoteDAO voteDao;
	
	@Get("show")
	public String show(Model model) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.tpglhdkz);
		return "/views/vote/voteList.vm";
	}

	@Post("getList")
	public String queryList(Vote vote) {
		JSONObject jo = new JSONObject();
		List<Vote> arr = voteDao.query(vote);
		for(Vote obj:arr){
			obj.setStartDate(DateUtils.getMinusTime(obj.getStartDate(), DateUtils.DATE_FULL_STR, 0));
			obj.setEndDate(DateUtils.getMinusTime(obj.getEndDate(), DateUtils.DATE_FULL_STR, 0));
			
			//起始时间---当前时间 >1 <-1 =0
			int startDateFlag = DateUtils.compareDateWithNow(DateUtils.parse(obj.getStartDate()));
			//结束时间---当前时间 >1 <-1 =0
			int endDateFlag = DateUtils.compareDateWithNow(DateUtils.parse(obj.getEndDate()));
			
			obj.setShowFlag(0);//默认为0
			
			//区间范围内
			if(startDateFlag>0 && endDateFlag<=0){
				obj.setShowFlag(1);
			}
			//在结束范围内
			if(startDateFlag<0 && endDateFlag>0){
				obj.setShowFlag(1);
			}
			
		}
		int size = voteDao.querysize(vote);
		jo.put("data", arr);
		jo.put("draw", vote.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@" + jo.toString();
	}
	
	
	@Get("createPage")
	public String createPage(Model model){
        //MemuEnum
        model.add("MemuEnum",MemuEnum.tpglhdkz);
		return "/views/vote/voteDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,Vote vote,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			voteDao.create(vote);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id){
		
		Vote vote =voteDao.queryById(id);
		
		vote.setStartDate(DateUtils.getMinusTime(vote.getStartDate(), DateUtils.DATE_FULL_STR, 0));
		vote.setEndDate(DateUtils.getMinusTime(vote.getEndDate(), DateUtils.DATE_FULL_STR, 0));
        //MemuEnum
        model.add("MemuEnum",MemuEnum.tpglhdkz);
        model.add("obj",vote);
		return "/views/vote/voteEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,Vote vote,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			voteDao.update(vote);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id){
		voteDao.deleteById(id);
		return "r:show";
	}
	//-----------------
	@Get("roleRefMenuEdit")
	public String roleRefMenuEdit(Model model, @Param("roleId") int voteId) {
		Vote vote =voteDao.queryById(voteId);
		String songIds = formSongIdsByVoteId(voteId);
		model.add("roleInfo", vote);
		model.add("menuIds", songIds);
		//MemuEnum
        model.add("MemuEnum",MemuEnum.tpglhdkz);
		return "/views/vote/voteRefSongEdit.vm";
	}	
	
	@Post("roleRefMenuInit")
	public String roleRefMenuInit(@Param("menuIds") String songIds) {
		JSONObject jo = new JSONObject();
		List<Song> menu = voteDao.getAll();
		List<Map> ret = new ArrayList<Map>();
		for (Song song : menu) {
			Map map = new HashMap();
			map.put("id", song.getId());
			map.put("pId", 0);
			map.put("name", song.getName());
			map.put("open", true);
			if (songIds != null && songIds.length() > 0 && (","+songIds).indexOf(","+song.getId()+",") != -1) {
				map.put("checked", true);
			} else {
				map.put("checked", false);
			}
			ret.add(map);
		}
		jo.put("ret", ret);
		return "@" + jo;
	}
	
	@Post("updateRoleMenuRef")
	public String updateRoleMenuRef(Vote vote, @Param("menuIds")String songIds) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		try {
			voteDao.deleteByVoteId(vote.getId());
			createRoleMenuRef(songIds,vote.getId());
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		return "@" + jo;

	}
	
	/**
	 * 根据活动期ID返回关联的音乐ID
	 * @param voteId
	 * @return
	 */
	private String formSongIdsByVoteId(int voteId) {
		List<String> arrSongIds = voteDao.getSongIdsByVoteId(voteId);
		String songIds = "";
		for (String songId : arrSongIds) {
			songIds += songId + ',';
		}
		return songIds;
	}
	
	private void createRoleMenuRef(String menuIds, int roleId) {
		if (menuIds != null && menuIds.length() > 0) {
			for (String menuId : menuIds.split(",")) {
				VoteSongRef voteSongRef = new VoteSongRef();
				voteSongRef.setVoteId(roleId);
				voteSongRef.setSongId(Integer.parseInt(menuId));
				voteSongRef.setType("1");
				voteDao.createVoteSongRef(voteSongRef);
			}
		}
	}
}